﻿import { Component, OnInit, Injector, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ImportRecordServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
@Component({
    selector: 'importRecordModal',
    templateUrl: './import-record-preview-modal.component.html'
})
export class ImportRecordModalComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    constructor(injector: Injector, private _importRecordService: ImportRecordServiceProxy) {
        super(injector);
    }

    active = false;
    loading = false;

    recordId;
    excelType;

    fileData: any;

    getExcelData(recordId): void {
        if (this.loading) {
            return;
        }

        this.loading = true;
        this.primengTableHelper.showLoadingIndicator();
        this._importRecordService.getImportExcelData(recordId).subscribe(result => {
            this.fileData = {
                fileName: '-',
                fileData: [
                    {
                        sheetName: '-',
                        sheetData: result.items
                    }
                ]
            };
            this.loading = false;

            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    show(excelType): void {
        this.active = true;

        this._importRecordService.getImportRecord(excelType).subscribe(result => {
            this.primengTableHelper.records = result;
            this.modal.show();
        });
    }

    getExcelDataRecord() {
        this.active = true;

        this.modal.show();
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
